'use strict';

const BaseController = require("./base");

class AccountController extends BaseController {
  /**
   * 更新账户信息
   */
  async updatePhoneId() {
    let { uId, phoneId } = this.ctx.request.query;
    let { idType } = this.ctx.helper;
    if (!phoneId) {
      this.faile("用户手机号码错误", -1003);
      return;
    }
    let res = await this.ctx.service.account.updateAccount(idType.wxId, uId, { phoneId });
    if (res.ok === 1) {
      this.success();
    } else {
      this.faile("更新用户失败", -1002);
    }
  }

  /**
   * 增加用户账户额度
   */
  async addAcount() {
    let { uId, changeAcount, appId, operaType = 0 } = this.ctx.request.query;
    //检查当前游戏是否有足够余额
    changeAcount = parseInt(changeAcount);
    if (changeAcount <= 0) {
      this.faile("游戏更改不了用户信息", -4000);
      return;
    }
    let gameMsg = await this.ctx.service.game.findOneGame(appId);
    if (gameMsg.aCount <= 0) {
      this.faile("游戏余额不足", -2000);
      return;
    }
    let gameAcount = gameMsg.aCount - changeAcount;
    gameAcount <= 0 ? gameAcount = 0 : 0;
    //更新游戏余额
    let gameResMsg = await this.ctx.service.game.updateGameMsg(appId, {
      aCount: gameAcount
    });
    if (gameResMsg.ok !== 1) {
      this.faile("游戏更新余额错误", -3001);
      return
    }
    let { idType } = this.ctx.helper;
    let accountMsg = await this.ctx.service.account.findOneAccount(idType.wxId, uId);
    if (!accountMsg) {
      this.faile("用户账户不存在", -3000);
      return;
    }
    let nowAcount = parseInt(accountMsg.aCount);
    let newAcount = nowAcount + changeAcount;
    let res = await this.ctx.service.account.updateAcount(idType.wxId, uId, {
      aCount: newAcount
    });

    let log = await this.ctx.service.accountLog.addLog({
      uId,
      appId,
      operaType,
      changeAcount
    });

    if (res.ok === 1 && log) {
      this.success({
        aCount: newAcount
      });
    } else {
      this.faile("更新额度失败", -1002);
    }
  }

  /**
   * 减少用户账户额度
   */
  async reduceAcount() {
    /**
     * 1，检查是否绑定手机号码
     * 2，输入验证码
     * 3，验证验证码
     * 4，发送手机号码
     * 5，验证手机号码，再次验证验证码
     */
    let { uId, changeAcount, operaType = 0 } = this.ctx.request.query;
    let { idType } = this.ctx.helper;
    changeAcount = parseInt(changeAcount);
    if (changeAcount >= 0) {
      this.faile("用户操作错误", -4001);
      return;
    }
    let accountMsg = await this.ctx.service.account.findOneAccount(idType.wxId, uId);
    if (!accountMsg) {
      this.faile("用户账户不存在", -3000);
      return;
    }
    let nowAcount = parseInt(accountMsg.aCount);
    let newAcount = nowAcount + changeAcount;
    let isZero = newAcount <= 0;
    isZero ? newAcount = 0 : 0;
    let res = await this.ctx.service.account.updateAcount(idType.wxId, uId, {
      aCount: newAcount
    });
    let log = await this.ctx.service.accountLog.addLog({
      uId,
      operaType,
      changeAcount: isZero ? nowAcount : changeAcount
    });

    if (res.ok === 1 && log) {
      this.success({
        aCount: newAcount
      });
    } else {
      this.faile("更新额度失败", -1002);
    }
  }

  /**
   * 获取账户信息
   */
  async findOneAccount() {
    let { uId } = this.ctx.request.query;
    let { idType } = this.ctx.helper;
    let oneAccount = await this.ctx.service.account.findOneAccount(idType.wxId, uId);
    if (oneAccount) {
      this.success(oneAccount);
    } else {
      this.faile("用户不存在", -1003);
    }
  }

  /**
   * 查找所有账户
   */
  async findUser() {
    let allUser = await this.ctx.service.account.findUser();
    this.success(allUser);
  }

}

module.exports = AccountController;
